# -*- coding: utf-8 -*-
# @Author : Yuan
from rest_framework.generics import ListAPIView, UpdateAPIView
from rest_framework.response import Response
from rest_framework import status
from apps.meiduo_admin.serializers.order import OrderInfoModelSerializer, OrderGoodsModelSerializer, \
    UpdateOrderInfoModelSerializer
from apps.meiduo_admin.utils import PageNum
from apps.orders.models import OrderInfo, OrderGoods


class OrderInfoListAPIView(ListAPIView):
    # queryset = OrderInfo.objects.all()
    serializer_class = OrderInfoModelSerializer
    pagination_class = PageNum

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return OrderInfo.objects.filter(order_id__contains=keyword)
        return OrderInfo.objects.all()


class OrderGoodsListAPIView(ListAPIView):
    serializer_class = OrderInfoModelSerializer

    def get_queryset(self):
        order_id = self.kwargs.get('order_id')
        return OrderInfo.objects.filter(order_id=order_id)

    def list(self, request, *args, **kwargs):
        instance = self.get_queryset()[0]
        if not instance:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = self.get_serializer(instance)
        return Response(serializer.data)


class OrderInfoUpdateAPIView(UpdateAPIView):
    queryset = OrderInfo.objects.all()
    serializer_class = UpdateOrderInfoModelSerializer
    lookup_field = 'order_id'

